<?php

namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use User\Entity\User;

class UserController extends AbstractActionController {

    /**
     * @var \Doctrine\ORM\EntityManager 
     */
    protected $entityManager;
    
    /**
     * @var \User\Repository\UserRepository
     */
    protected $userRepository;
    
    protected $userForm;

    public function indexAction() {
        return new ViewModel(array(
            'users' => $this->getUserRepository()->findAll(),
        ));
    }

    public function addAction() {
        $user = new User();
        
        $form = $this->getUserForm();
        $form->get('submit')->setValue('Add');
        $form->setAttribute('action', $this->url()->fromRoute('user', array('action' => 'add')));
        $form->bind($user);
        
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $this->getEntityManager()->persist($user);
                $this->getEntityManager()->flush();
                return $this->redirect()->toRoute('user');
            }
        }
        return array('form' => $form);
    }

    public function editAction() {
        // TODO: ADD ROLE
        // TODO: ADMIN ROLE SHOULD NOT BE EDITED!
        // TODO: route types and parameters
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('user', array('action' => 'add'));
        }
        $user = $this->getUserRepository()->find($id);

        $form = $this->getUserForm();
        $form->bind($user);
        $form->get('submit')->setAttribute('value', 'Edit');
        $form->setAttribute('action', $this->url()->fromRoute('user', array(
                    'action' => 'edit',
                    'id' => $id,
        )));

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $this->getEntityManager()->persist($user);
                $this->getEntityManager()->flush();
                return $this->redirect()->toRoute('user');
            }
        }

        return array(
            'user' => $user,
            'form' => $form,
        );
    }

    public function deleteAction() {
        // TODO: CANT DELETE ADMIN ACCOUNT!
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('user');
        }
        
        $user = $this->getUserRepository()->find($id);

        $request = $this->getRequest();
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');

            if ($del == 'Yes') {
                $id = (int) $request->getPost('id');
                $this->getEntityManager()->remove($user);
                $this->getEntityManager()->flush();
            }

            // Redirect to list of users
            return $this->redirect()->toRoute('user');
        }

        return array(
            'user' => $user,
        );
    }

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEntityManager() {
        if (!$this->entityManager) {
            $this->entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->entityManager;
    }
    
    /**
     * @return \User\Repository\UserRepository
     */
    public function getUserRepository() {
        if (!$this->userRepository) {
            $this->userRepository = $this->getServiceLocator()->get('User\Repository\UserRepository');
        }

        return $this->userRepository;
    }

    public function getUserForm() {
        if (!$this->userForm) {
            $this->userForm = $this->getServiceLocator()->get('User\Form\UserForm');
        }
        return $this->userForm;
    }

}